﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PlanTasks.Commons;
using System.IO;

namespace PlanTasks.Tool
{
    public partial class PlanTaskManagerConfigView : Form
    {
        private readonly PlanTaskManagerConfig _PlanTaskManagerConfig;
        private readonly string _OldPath;
        private string _NewPath;
        private readonly PlanTaskList _PlanTaskList;
        private readonly List<PlanTask> _PlanTasks;

        public PlanTaskManagerConfigView()
        {
            InitializeComponent();
            _PlanTaskList = PlanTaskList.GetObject();
            _PlanTaskManagerConfig = PlanTaskManagerConfig.GetObject();
            this.PathText.Text = _PlanTaskManagerConfig.SyncPath.Equals(".")? AppDomain.CurrentDomain.BaseDirectory:_PlanTaskManagerConfig.SyncPath;
            _OldPath = this.PathText.Text;
            _NewPath = _OldPath;
            this.MinimizeCheck.Checked = _PlanTaskManagerConfig.Minimize?true:false;
            _PlanTasks = _PlanTaskList.PlanTasks;
        }

        private void ClearData_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("您真的要删除吗？", "此删除不可恢复", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                _PlanTaskList.DeleteAllPlanTask();
            }

        }

        private void ChangePath_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog path = new FolderBrowserDialog();
            path.ShowDialog();
            if (string.IsNullOrEmpty(path.SelectedPath))
            {
                return;
            }
            this.PathText.Text = path.SelectedPath;
            _NewPath = this.PathText.Text;
        }

        private void ConfigYes_Click(object sender, EventArgs e)
        {
            //获取最小化配置项根据配置项写入对应配置
            _PlanTaskManagerConfig.ChangeMinimize(this.MinimizeCheck.Checked);

            //如果路径没有更改什么都不做
            if (_NewPath.Equals(_OldPath))
            {
                this.Close();
                return;
            }
            //判断新路径是否有该文件，如果有弹出提示，如果没有移动文件，同时修改程序内变量，修改配置文件。
            if (File.Exists(Path.Combine(_NewPath, "PlanTaskList.xml")))
            {
                MessageBox.Show("目标路径已经有一个数据文件，请重新指定路径！");
                return;
            }

            _PlanTaskManagerConfig.ChengeSyncPath(_NewPath);

            this.Close();
        }

        private void ConfigNo_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
